package com.rzjm.api.service.impl;

import com.rzjm.api.dao.*;
import com.rzjm.api.entity.*;
import com.rzjm.api.exception.TransactionalException;
import com.rzjm.api.service.TerminalCheckRecordService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;


@Service
public class TerminalCheckRecordServiceImpl extends BaseServiceImpl<TerminalCheckRecordDao, TerminalCheckRecord>
        implements TerminalCheckRecordService {

    @Resource
    private UserDao userDao;
    @Resource
    private SysUserDao sysUserDao;
    @Resource
    private CkTerminalDao ckTerminalDao;
    @Resource
    private TerminalFittingDao terminalFittingDao;
    @Resource
    private TerminalCheckRecordDao terminalCheckRecordDao;
    @Resource
    private TerminalCheckRecordImgDao terminalCheckRecordImgDao;

    @Value("${web.imgprefix}")
    private String imgprefix;//图片地址
    @Override
    public List<TerminalCheckRecord> getListBySysUserId(User user, Integer is_clean, Integer check_state,
                                                        Integer offset, Integer pageSize) {
        Map<String,Object> map  = new HashMap<>();
        SysUser sysUser = sysUserDao.selectById(user.getSysUserId());

        if(sysUser != null){
            if(sysUser.getUserType() >= 4){
                map.put("shop_id",sysUser.getShopId());
            } else if(sysUser.getUserType() == 2){
                map.put("jituan_user_id",sysUser.getShopId());
            } else if(sysUser.getUserType() == 3){
                map.put("sys_user_id",sysUser.getShopId());
            }
        }
        if(check_state == -1){//查询全部的
//            map.put("sys_user_id",sysUser.getUserId());
            map.put("is_clean",is_clean);
        }else{//指定 是否是 报修
            map.put("check_state",check_state);
        }
        List<TerminalCheckRecord> list = new ArrayList<>();//getPageEntityList(qw,page,pageSize);
        list = terminalCheckRecordDao.getPageByUserId(map,offset,pageSize);
        list.forEach(record -> {
            User user1 = userDao.selectById(record.getWxUserId());
            if(user1!=null){
                SysUser sysUser1 = sysUserDao.selectById(user1.getSysUserId());
                if(sysUser1!=null){
                    record.setSysUserName(sysUser1.getName());
                }
            }
            List<TerminalCheckRecordImg> imgs = terminalCheckRecordImgDao.selectListByColumn("record_id",record.getId());
            if(imgs!=null){
                imgs.forEach(img->{
                    img.setImgUrl(imgprefix +img.getImgUrl());
                });
            }
            record.setImgs(imgs);
        });
        return list;
    }

    @Override
    @Transactional
    public int addRecord(TerminalCheckRecord record, CkTerminal terminal) throws TransactionalException {
        try{
            int ret = terminalCheckRecordDao.insert(record);
            if(ret <= 0){
                throw new TransactionalException("ckTerminalCheckRecordDao insert error");
            }
            if(record.getCheckState() == 2){//报修
                terminal.setState(2);
                terminal.setLastRepairRecordId(record.getId());
                terminal.setLastRepairReportTime(new Date());
                ret = ckTerminalDao.updateById(terminal);
                if(ret <= 0){
                    throw new TransactionalException("ckTerminalDao update error");
                }
            }
            if(record.getIsClean() == 2){//保养
                TerminalFitting fitting = terminalFittingDao.selectById(record.getFittingId());
                if(fitting != null){
                    fitting.setLastHealthTime(new Date());//修改最后一次的保养时间
                    ret = terminalFittingDao.updateById(fitting);
                    if(ret <= 0){
                        throw new TransactionalException("ckTerminalFittingDao update error");
                    }
                }
            }
            return ret;
        }catch (Exception ex){
            throw new TransactionalException("addRecord system error");
        }
    }
}
